﻿@using CleverCMS.Core.Extensions
@using CleverCMS.Cms.Data.Entities
@model CleverCMS.Cms.Services.ViewModels.PageCommentsViewModel
@functions
{
    private string str = "";
    public string CreateNode(CmsPageComment node)
    {
        var selNodes = Model.Comments.Where(x => x.ParentId == node.Id).ToList();
        
        if (!selNodes.Any()) 
            return "";

        foreach (var selNode in selNodes)
        {
            var childNode = new CmsPageComment { Id = selNode.Id };
            str += "<hr/><div class='media'><a href='#' class='pull-left'><img alt='' src='" +
                   "http://www.gravatar.com/avatar/" + selNode.Email.ToLowerInvariant().EncryptMD5().ToLowerInvariant() + "?d=monsterid" + "' class='media-object'></a><div class='media-body'>" +
                   "<h4 class='media-heading'>" + 
                   selNode.Name + 
                   "</h4><span>" + 
                   selNode.CreatedAt.ToShortDateString() + " " + 
                   selNode.CreatedAt.ToShortTimeString() + " | <a href='#' class='reply' rel='id_" + 
                   selNode.Id + "'>Reply</a></span>" +
                   "<p>" + selNode.Comment + "</p>";
            
            CreateNode(childNode);
           
            str += "</div></div>";
        }
        return str;
    }
}

<span id="com"></span>

@if (Model.Comments.Count() != 0)
{
    <h3>Comments</h3>
}
else
{
    <h3>Have no comments</h3>
}
<hr />
@{
    if (Model.Comments.Count() != 0)
    {
        var nodes = Model.Comments.Where(x => x.ParentId == null).ToList();
        foreach (var node in nodes)
        {
            var root = new CmsPageComment {Id = node.Id};
            <div class="media">
                <a href="#" class="pull-left">
                    <img alt="" src="http://www.gravatar.com/avatar/@(node.Email.ToLowerInvariant().EncryptMD5().ToLowerInvariant())?d=monsterid" +" class="media-object">
                </a>
                <div class="media-body">
                    <h4 class="media-heading">@node.Name</h4>
                    <span>@node.CreatedAt.ToShortDateString() @node.CreatedAt.ToShortTimeString() | <a href="#" class="reply" rel="id_@node.Id">Reply</a></span>
                    <p>@node.Comment</p>
                    @Html.Raw(CreateNode(root))
                </div>
            </div>
            <hr/>
            str = "";
        }
    }        
}

<div class="post-comment" id="post-comment">
    <h3 id="@Model.Id">Post Comments</h3> 
    <p id="quoting" style="font-style: italic"></p>
    <form id="myform">
        <label>Name</label>
        <input type="text" class="span7" id="name">
        <label>Email <span class="color-red">*</span></label>
        <input type="text" class="span7" id="email"><span id="valid" style="margin-left: 20px;"></span>
        <label>Message</label>
        <textarea class="span10" rows="8" id="message"></textarea>
        <p><button class="btn" type="submit" id="post">Post Comment</button></p>
    </form>
</div>

<script type="text/javascript" src="@Url.Content("~/themes/default/scripts/cms/public/page_comments.js")"></script>
